package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.dao.HolderDao;
import com.example.domain.Holder;
import com.example.domain.R;
import com.example.service.HolderService;
import lombok.extern.slf4j.Slf4j;
import com.example.domain.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;


@Slf4j
@Service
public class HolderServiceImpl implements HolderService {
    @Autowired
    private HolderDao holderDao;


    @Autowired
    RestTemplate restTemplate;

    @Override
    public void saveHolder(@Param("holder") Holder holder) {
        holderDao.saveHolder(holder);
    }

    @Override
    public Holder selectById(String holderId) {
        return holderDao.selectById(holderId);
    }

    @Override
    public User getData(Object pageNum, Object pageSize, Object pram1, Object pram2) {
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        log.info("pram1:{}",pram1);
        log.info("pram2:{}",pram2);

        String url = "http://192.168.31.12:8081/interface/test";

        HashMap<String, Object> reqMap = new HashMap<>();
        reqMap.put("pageNum",pageNum);
        reqMap.put("pageSize",pageSize);
        reqMap.put("pram1",pram1);
        reqMap.put("pram2",pram2);
        log.info("reqMap:{}",reqMap);

        /*JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageNum",pageNum);
        jsonObject.put("pageSize",pageSize);
        jsonObject.put("pram1",pram1);
        jsonObject.put("pram2",pram2);

        String respStr = HttpClientToInterface.doPost(url, jsonObject);
        User user = JSON.parseObject(respStr, User.class);
        log.info("user:{}",user);*/


        //获取第三方返回的json字符串
        String postForObject = restTemplate.postForObject(url, reqMap, String.class);
        log.info("postForObject:{}",postForObject);

        //转为R对象
        R r = JSON.parseObject(postForObject, R.class);

        //获取R对象中的data
        Object data = r.getData();
        log.info("r:{}",r);

        //data转为字符串
        String dataStr = JSON.toJSONString(data);
        log.info("data:{}",data);

        //data字符串转为user对象
        User user = JSON.parseObject(dataStr, User.class);

        return user;
    }
}
